Namespacing everything to /UVa.
[andmenj-acm.git] / UVa / 673 - Parentheses balance / 673.cpp
blob3f8882246ebaff837b9dbefb6801d7b201c7007f
1 #include <string>
2 #include <iostream>
4 using namespace std;
6 bool valid(const string s){
7 //cout << s << endl;
8 if (s == "") return true;
10 int p=0, b=0, i;
11 for (i = 0; i<s.size(); ++i){
12 if (s[i] == '(') ++p;
13 if (s[i] == ')') --p;
14 if (s[i] == '[') ++b;
15 if (s[i] == ']') --b;
17 if (p == 0 && b == 0){
18 break;
23 if (i < s.size()-1){
24 return (valid(s.substr(0, i+1)) && valid(s.substr(i+1)));
27 char first = s[0];
28 char last = s[s.size() - 1];
29 if ( (first == '(' && last == ')') || (first == '[' && last == ']') ){
30 return (valid(s.substr(1, s.size()-2)));
32 return false;
35 int main(){
36 int n;
37 cin >> n;
38 while (getchar() != '\n');
39 string s;
40 while (n--){
41 getline(cin, s);
42 cout << (valid(s)?"Yes":"No") << endl;
44 return 0;